package cn.gqr.controller;

import cn.gqr.entity.CartOrder;
import cn.gqr.entity.Order;
import cn.gqr.entity.OrderDetail;
import cn.gqr.entity.OrderInfo;
import cn.gqr.service.OrderService;
import cn.gqr.tool.RespJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class OrderController {

    @Autowired
    private OrderService orderService;


    //获取当前登录用户的所有订单信息并且分页以及条件查询
    @PreAuthorize("hasAuthority('user')")
    @RequestMapping("/order/findOrderByPage/{start}/{num}")
    public Map<String,Object> selectUserOrderAllPage(@PathVariable("start") int start,
                                      @PathVariable("num") int num,
                                      Integer isPayment ,String startingTime,String endTime){
        List<OrderInfo> orders = orderService.selectUserOrderAllPage(isPayment, startingTime, endTime, start, num);
        return RespJson.pack(200,"订单详情",orders);
    }

    //更新发货地址及电话
    @PreAuthorize("hasAuthority('user')")
    @RequestMapping("/order/upAdrPhone")
    public Map<String,Object> updateAddressAndPhoneByOrderId(@RequestBody Order order){
        boolean b = orderService.updateAddressAndPhoneByOrderId(order);
        return RespJson.pack(200,b?"更新成功":"未知错误",null);
    }

    //更新订单付款状态
    @PreAuthorize("hasAuthority('user')")
    @RequestMapping("/order/upPaymentStatus")
    public Map<String,Object> updatePaymentStatus(@RequestBody Order order){
        if(order.getId()==null){
            return RespJson.pack(200,"参数不能为空",null);
        }
        boolean b = orderService.updatePaymentStatusByOrderId(order);
        return RespJson.pack(200,b?"付款成功":"付款失败",null);
    }

    //添加订单添加订单明细
    @PreAuthorize("hasAuthority('user')")
    @RequestMapping("/order/insert")
    public Integer insert(@RequestBody CartOrder cartOrder){
        System.out.println(cartOrder);
        Integer orderId = orderService.insert(cartOrder);

        return  orderId;
    }



}
